<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Campaignagencyplace extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['controller'] = 'campaignagencyplace';
        $this->load->model('Mcampaignagencyplace');
        $this->load->model('Mindex');
    }
    
    public function place()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,4)); // admin và agency-place
        $id_place = $this->session->userdata('place');
        if(!in_array($id_place, array(null, '', '0')))
        {
            $this->data['id_active'] = $this->getParamUri(3,3);
            $this->load->library('pagination');
            $config['per_page'] = $this->data['per_page'] = 10;
            $this->data['page'] = $this->getParamUri(3,3);
            $data = $this->Mcampaignagencyplace->camPaignPlace($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'), $id_place);
            $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');//$this->uri->segment(2).'/');
            $config['total_rows'] = $data['count'];
            $config['uri_segment'] = 3;
            $config['uri_segment_page'] = $this->getParamUri(3,3);
            $config['suffix'] = '.html';
            $this->pagination->initialize($config);
     
            $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
            $this->data['data'] = $data['data'];
    
            $this->data['action'] = 'campaignplace';
            $this->load->view('temp/layout', $this->data);   
        }else{
            redirect('reject-request');
        }
    }
    
    public function send()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaignagencyplace->getCampaignAgencysendDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        //$this->data['static'] = $this->Mindex->getStatic($data['idCampaign']);
        
        $this->data['action'] = 'send';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function pendding()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaignagencyplace->getCampaignAgencyPenddingDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        $this->data['action'] = 'pendding';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function running()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaignagencyplace->getCampaignAgencyDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        //$this->data['static'] = $this->Mindex->getStatic($data['idCampaign']);
        
        $this->data['action'] = 'running';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function other()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaignagencyplace->getCampaignAgencyOtherDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        //$this->data['static'] = $this->Mindex->getStatic($data['idCampaign']);
        
        $this->data['action'] = 'other';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function viewcampaignagency()
    {
        $this->load->library('form_validation');
		$this->form_validation->set_rules('status', 'Trạng thái', 'required');
        
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['campaign_id'] = $id = $this->getParamInt('id');
        $this->data['data'] = $this->Mcampaignagencyplace->viewCampaignAgencyDB($id);
        $this->data['getListActive'] = $this->Mcampaignagencyplace->getListActive($this->data['data']->active,$this->data['data']->status_old);
        
        if($this->form_validation->run() === TRUE)
        {
			$status = $this->getParamInt('status');
			$run = $this->Mcampaignagencyplace->updateCampaign($status,$this->data['campaign_id']);
			if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'view-campaign-user.html?id='.$id.'";</script>');
            }
        }
        
        $this->data['action'] = 'viewcampaignagency';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function ajaxlockcampaign()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        die(json_encode($this->Mcampaignagencyplace->ajaxLockCampaignDB($this->getParamInt('id'))));
    }
    
    public function ajaxcancelcampaign()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        die(json_encode($this->Mcampaignagencyplace->ajaxCancelCampaignDB($this->getParamInt('id'))));
    }
    
    public function ajaxsendapprovalcampaign()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        die(json_encode($this->Mcampaignagencyplace->ajaxSendApprovalCampaignDB($this->getParamInt('id'))));
    }
    
    public function ajaxrestorecampaign()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        die(json_encode($this->Mcampaignagencyplace->ajaxRestoreCampaignDB($this->getParamInt('id'))));
    }
	
    
    
    public function placeagencyplace()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,4)); // admin và agencyplace
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['place'] = $this->Mcampaignagencyplace->getPlace();
        //echo "<pre>";die(print_r($this->data['place']));
        $this->data['action'] = 'placeagencyplace';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function report()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,4)); // admin va agencyplace
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mcampaignagencyplace->getCountAgency($this->session->userdata('id_user'));
        $campaignReport = $this->Mcampaignagencyplace->getCampaignReport();
        
        $this->data['campaignReport'] = $campaignReport['data'];
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;

        $this->data['action'] = 'report';
        $this->load->view('temp/layout', $this->data);
    }
    
    
    public function ajaxgetcampaignreport(){
        $last_id = $this->getParamInt('last_id');
        $campaignReport = $this->Mcampaignagencyplace->ajaxGetCampaignReport($last_id);
        die(json_encode($campaignReport['data']));
    }
    
    public function statistic()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $time_select = $this->getParamString('time_select');
    
        
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['type'] = $this->getParamInt('type');
        $this->data['chart_ab'] = $this->Mcampaignuser->getChartAb($this->data['c_id'], $this->data['time'], $this->data['type'], $this->session->userdata('id_user'));
        
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 25;
        $this->data['page'] = $this->getParamUri(2,3);
        $this->data['per_page'] = $config['per_page'];
        if(!in_array($this->data['c_id'], array(null, '', '0')) || !in_array($this->data['time_select'], array(null, '', '0')))
        {
            $config['paramhack'] = '?id='.$this->data['c_id'].'&type='.$this->data['type'].'&time='.$this->data['time'].'&time_select='.$this->data['time_select'].'#page';
        }
        
        $offer = (($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0);
        $id_user = $this->session->userdata('id_user');
        $data = $this->Mcampaignuser->getLogAccess($config['per_page'],$offer,$id_user,$this->data['c_id'],$this->data['time']);
   
        
        $config['base_url'] = base_url('page_ajax_detail/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $config['ellipsize'] = TRUE;
        $config['stt_reduced']  = 10;
        $config['reduced']  = 10;
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        
        $_data = $this->Mcampaignuser->getCount($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);
        //echo "<pre>";die(print_r($_data));
        $this->data['browser'] = $_data->browser;
        $this->data['browser_ver'] = $_data->browser_ver;
        $this->data['device_name'] = $_data->device_name;
        $this->data['os_group'] = $_data->os_group;
        
        
        $this->data['show_time'] = $this->Mcampaignuser->getCountTime($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id'], 20, 0);
        
             
             
        $this->data['getmail'] = $this->Mcampaignuser->getmail($this->data['c_id'], $this->data['c_id'], $this->data['time']);
        $this->data['getfacebook'] = $this->Mcampaignuser->getfacebook($this->data['c_id'], $this->data['c_id'], $this->data['time']);
		$this->data['getmobile'] = $this->Mcampaignuser->getmobile($this->data['c_id'], $this->data['c_id'], $this->data['time']);
        
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
       //echo "<pre>";die(print_r($this->data));
        //$this->data['data'] = $this->Mcontact->getdetail($id);
        
        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        $this->data['action'] = 'statistic';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function selectType($id)
    {
        $arr = (object)array('Tất cả','Trình duyệt','Thiết bị','Hệ điều hành');
        $html  = '';
        foreach($arr AS $el=>$le)
        {
            if($id == $el)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$el.'"'.$selected.'>'.$le.'</option>';
        }
        return $html;
    }
    
    
    public function userexportreportplace()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExport($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/userexportreportplace', $this->data);
    }
    
    public function gettimeaccess()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        
        $time_select = $this->getParamString('time_select');
            
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['show_time'] = $this->Mcampaignuser->getCountTime($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id'], 20, $this->getParamInt('dem'));
        if(in_array($this->data['show_time'], array(null, '', '0')))
        {
            die('201');
        }
        //echo $this->getParamInt('dem');
        //die(print_r($this->data['show_time']));
        $this->load->view('temp/statics/gettimeaccess', $this->data);
    }
    
    public function exportreportemail()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExportEmail($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/exportreportemail', $this->data);
    }
    
    public function exportreportfacebook()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExportFacebook($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/exportreportfacebook', $this->data);
    }
    
    public function exportreportmobile()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExportMobile($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/exportreportmobile', $this->data);
    }
    
    public function popupreporting()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,4)); // admin + agencyplace
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mcampaignagencyplace->getCountAgency($this->session->userdata('id_user'));
        $campaignReport = $this->Mcampaignagencyplace->getReportExport();
        
        $this->data['campaignReport'] = $campaignReport['data'];
        //$this->data['listCustomers'] = $this->Mcampaignagencyplace->getListCustomers();
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;

        $this->data['action'] = 'reportexport';
        $this->load->view('temp/campaignagencyplace/reportexport', $this->data);
    }
    
    public function ajaxpopupreporting()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2));
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mcampaignagencyplace->getCountAgency($this->session->userdata('id_user'));
        
        $times = $this->getParamString('times');
        $times_start = $this->getParamString('times_start');
        $times_end = $this->getParamString('times_end');
        $customer_id = $this->getParamString('customer_id');
        $campaignReport = $this->Mcampaignagencyplace->getAjaxReportExport($times,$times_start,$times_end,$customer_id);
        
        $this->data['campaignReport'] = $campaignReport['data'];
        $this->data['listCustomers'] = $this->Mcampaignagencyplace->getListCustomers();
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
        $this->data['action'] = 'ajaxreportexport';
        $this->load->view('temp/campaignagency/ajaxreportexport', $this->data);
    }
}